n=int(input())
ids=list(map(int,input().split(",")))
scores=list(map(int,input().split(",")))

def getResult():
    players={}
    for i in range(0,n):
        players.setdefault(ids[i],[])
        players.get(ids[i]).append(scores[i])

    ans=[]
    for pid in players:
        if len(players[pid])>=3:
            players[pid].sort(reverse=True)
            ans.append((pid,sum(players[pid][:3])))

    print(ans)
    ans.sort(key=lambda x:(-x[1],-x[0]))
    print(ans)
    return ",".join(map(lambda x:str(x[0]),ans))

13
3,3,7,4,4,4,4,7,7,3,5,5,5
53,80,68,24,39,76,66,16,100,55,53,80,55

print(getResult())